<!doctype html>
<!--
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
  <meta charset="utf-8">
  <script src="../../node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js"></script>
  <script src="wct-browser-config.js"></script>
  <script src="../../node_modules/wct-browser-legacy/browser.js"></script>
  <script type="module" src="../../lib/utils/import-href.js"></script>
<body>

<x-test></x-test>

<script type="module">
import { importHref } from '../../lib/utils/import-href.js';
import { PolymerElement } from '../../polymer-element.js';
import { Base } from '../../polymer-legacy.js';

suite('importHref', function() {

  let parsedEl = document.querySelector('x-test');

  test('importing polymer via Polymer.importHref', function(done) {
    assert.notOk(PolymerElement);
    importHref('../../polymer.html', function() {
      assert.ok(PolymerElement);
      done();
    });
  });

  test('element loaded with Polymer.Base.importHref upgrades properly', function(done) {
    Base.importHref('sub/x-test.html', function() {
      assert.ok(parsedEl.$.sub, 'parsed element not upgraded with template when import loaded');
      assert.notOk(parsedEl.$.sub.$, 'sub element of imported element should not yet be upgraded');
      var el = document.createElement('x-test');
      document.body.appendChild(el);
      assert.ok(el.$.sub, 'imperatively created element not upgraded with template when import loaded');
      done();
    });
  });

  test('sub-element loaded with instance importHref upgrades properly', function(done) {
    parsedEl.importHref('sub/x-sub.html', function() {
      assert.equal(this, parsedEl);
      assert.ok(parsedEl.$.sub.$.test, 'sub element not upgraded when import loaded');
      done();
    });
  });

  test('load is triggered on second call', function(done) {
    Base.importHref('sub/x-test.html', function() {
      Base.importHref('sub/x-test.html', function() {
        done();
      });
    });
  });

});
</script>

</body>
</html>
